package com.dofast.bt.mvp.view.fragment.mian;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dofast.bt.R;
import com.dofast.bt.adapter.BasketAdapter;
import com.dofast.bt.adapter.SelectCallBack;
import com.dofast.bt.base.BaseFragment;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.bean.ShoppingCartBean;
import com.dofast.bt.mvp.model.IMainModel;
import com.dofast.bt.mvp.model.MainModel;
import com.dofast.bt.mvp.view.activity.basket.EditAddressActivity;
import com.dofast.bt.mvp.view.activity.basket.StoreDetailActivity;
import com.dofast.bt.mvp.view.activity.basket.SubmitOderActivity;
import com.dofast.bt.mvp.view.activity.shop.TeaDetailActivity;
import com.dofast.bt.util.Helper;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * username:Created by hsj on 2016/12/7 0007.
 * class：
 * 类描述：茶篓
 */

public class BasketFragment extends BaseFragment implements AdapterView.OnItemClickListener{


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.lv_basket)
    ListView lvBasket;
    @BindView(R.id.iv_selcect_all)
    ImageView ivSelcectAll;
    @BindView(R.id.tv_select_all)
    TextView tvSelectAll;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.ll_select_all)
    LinearLayout llSelectAll;
    private BasketAdapter adapter;
    private List<ShoppingCartBean> list;
    private IMainModel mainModel = null;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_basket;
    }

    @Override
    protected void initView() {
        list = new ArrayList<ShoppingCartBean>();
    }

    @Override
    protected void initData() {
        mainModel = new MainModel();
        adapter = new BasketAdapter(list, new SelectCallBack() {
            @Override
            public void select(int position, boolean flags) {
                //// TODO: 2016/12/9 0009 商品选择逻辑处理

            }
        });
        lvBasket.setAdapter(adapter);
        //获取数据
        getData();
        //
        lvBasket.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (list.get(position).isCheck()) {
                    list.get(position).setCheck(false);
                } else {
                    list.get(position).setCheck(true);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 获取数据
     */
    private void getData() {
        try {
            list.clear();
            list.addAll(mainModel.getShoppingCar());
            adapter.notifyDataSetChanged();
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    /**
     * 显示全选
     */
    private void showSelcetctAll() {
        llSelectAll.setVisibility(View.VISIBLE);
        tvEdit.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏全选
     */
    private void hideSelectAll() {
        llSelectAll.setVisibility(View.GONE);
        tvEdit.setVisibility(View.GONE);
    }

    /**
     * @param isCheck 全选
     */
    private void selectAll(boolean isCheck) {

        if (isCheck) {
            ivSelcectAll.setImageResource(R.mipmap.select_normal);
        } else {
            ivSelcectAll.setImageResource(R.mipmap.select_default);
        }
        for (ShoppingCartBean good : list) {
            good.setCheck(isCheck);
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 编辑
     */
    private void edit() {
        if (isEdit) {
            isEdit = false;
            tvEdit.setText("完成");
            tvCommit.setText("删除");

        } else {
            isEdit = true;
            tvEdit.setText("编辑");
            tvCommit.setText("结算");

        }
    }

    /**
     * 删除逻辑
     */
    private void delete() {
        try {
            for (ShoppingCartBean sc:getGoods()
                 ) {
                mainModel.deleteCar(sc.getGoodsId());
            }
            getData();
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    private boolean isCheck = true;
    private boolean isEdit = true;

    @OnClick({R.id.tv_edit, R.id.iv_selcect_all, R.id.tv_select_all, R.id.tv_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_edit:
                edit();
                break;
            case R.id.iv_selcect_all:
                if (isCheck) {
                    selectAll(true);
                    isCheck = false;
                } else {
                    selectAll(false);
                    isCheck = true;
                }
                break;
            case R.id.tv_select_all:
                if (isCheck) {
                    selectAll(true);
                    isCheck = false;
                } else {
                    selectAll(false);
                    isCheck = true;
                }

                break;
            case R.id.tv_commit:
                String commit = tvCommit.getText().toString().trim();
                if (commit.equals("结算")) {
                    // TODO: 2016/12/9 0009 结算逻辑处理
                    if (null == list || list.isEmpty()) {

                    } else {
                        try {
                            SubmitOderActivity.gotoSubmitOrderActivity(getActivity(), getGoods());
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    delete();
                }
                break;
        }
    }

    /**
     * @return 选中的商品
     */
    private List<ShoppingCartBean> getGoods() throws DbException {
        List<ShoppingCartBean> goods = new ArrayList<ShoppingCartBean>();

        for (ShoppingCartBean good : list) {
            if (good.isCheck()) {
                goods.add(good);
            }
        }
        return goods;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
